package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProSchoolMiens;
import com.shengde.jinwawaforleader.network.protocolbean.ProShowClickGood;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 编辑 学校风采的内容  的编辑界面
 * Created by JYQ on 2017/1/9 0009.
 */
public class EditSchoolMienActivity extends BaseActivity {
    private static final String TAG = "EditSchoolMienActivity";
    private TextView tv_back;
    private TextView tv_title;
    private TextView fill_add;
    private String type;
    private EditText edt_school_mien;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_edit_school_mien);

    }

    @Override
    public void initView() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        fill_add = (TextView) findViewById(R.id.fill_add);
        edt_school_mien = (EditText) findViewById(R.id.edt_school_mien);
    }

    @Override
    public void initData() {
        fill_add.setText(getString(R.string.save));
        type = getIntent().getStringExtra("type");
        if ("1".equals(type)) {

            tv_title.setText(getString(R.string.tv_kingarden_name));
        } else if ("2".equals(type)) {
            tv_title.setText(getString(R.string.tv_kingraden_address));

        } else if ("3".equals(type)) {
            tv_title.setText(getString(R.string.tv_link_telephone));

        } else if ("4".equals(type)) {
            tv_title.setText(getString(R.string.tv_school_introduce));
        }

    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
        fill_add.setOnClickListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.fill_add:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                if ("1".equals(type)) {
                    bundle.putString("name", edt_school_mien.getText().toString().trim());
                    intent.putExtras(bundle);
                    setResult(Constant.SETTING_KINGARDEN_NAME, intent);
                    modificationKingardenName(edt_school_mien.getText().toString().trim(), null, null, null);
                } else if ("2".equals(type)) {
                    bundle.putString("address", edt_school_mien.getText().toString().trim());
                    intent.putExtras(bundle);
                    setResult(Constant.SETTING_KINGARDEN_ADDRESS, intent);
                    modificationKingardenName(null, edt_school_mien.getText().toString().trim(), null, null);

                } else if ("3".equals(type)) {
                    bundle.putString("telephone", edt_school_mien.getText().toString().trim());
                    intent.putExtras(bundle);
                    setResult(Constant.SETTING_LINK_TELEPHONE, intent);
                    modificationKingardenName(null, null, edt_school_mien.getText().toString().trim(), null);

                } else if ("4".equals(type)) {
                    bundle.putString("introduce", edt_school_mien.getText().toString().trim());
                    intent.putExtras(bundle);
                    setResult(Constant.SETTING_SCHOOL_INTRODUCUTION, intent);
                    modificationKingardenName(null, null, null, edt_school_mien.getText().toString().trim());
                }
                break;
        }
    }

    /**
     * 判断是否为空
     */
    private void judgeIsNull() {
        if (User.getSiteid(EditSchoolMienActivity.this) == null || "".equals(User.getSiteid(EditSchoolMienActivity.this))) {
            showSchoolMien();
        }else {
            finish();
        }
    }


    /**
     * 获取校园风采相关信息
     */
    private void showSchoolMien() {
        String url = "";
        if (User.isTest) {
            url = BaseProtocol.HOST_BASE_TEST + "/synopsis/synopsisList";
        } else {
            url = BaseProtocol.HOST_BASE + "/synopsis/synopsisList";
        }
        RequestParams params = BaseProtocol.getRequestParams(url);
        params.addParameter("schoolid", User.getSchoolId(this));

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ProSchoolMiens proSchoolMiens = new Gson().fromJson(result, ProSchoolMiens.class);
                if (proSchoolMiens != null && proSchoolMiens.code == 0) {
                    if (proSchoolMiens.sitelist.size() > 0) {
                        ProSchoolMiens.SitelistEntity sitelistEntity = proSchoolMiens.sitelist.get(0);
                        Log.i(TAG, "onSuccess: id= " + sitelistEntity.id);
                        User.setSiteid(EditSchoolMienActivity.this, sitelistEntity.id + "");
                        User.setSchoolMienUrl(EditSchoolMienActivity.this, sitelistEntity.url);
                        User.setType(EditSchoolMienActivity.this, sitelistEntity.type);
                        finish();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }


            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

    /**
     * 上传学校名字  到服务器
     *
     * @param trim
     * @param s2
     * @param s1
     * @param s
     */
    private void modificationKingardenName(String trim, String s2, String s1, String s) {
        RequestParams params = BasexUtilsProtocol.sendHttpRequest(this, "/synopsis/synopsisAdd");
        params.addParameter("type", "2");
        params.addParameter("schoolid", User.getSchoolId(this));
        params.addParameter("sname", trim);
        params.addParameter("address", s2);
        params.addParameter("remark", s);
        params.addParameter("tel", s1);
        Log.i(TAG, "modificationKingardenName: s2 = " + s2);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ProShowClickGood proShowClick = new Gson().fromJson(result, ProShowClickGood.class);
                if (proShowClick.getCode() == 0) {
                    Toast.makeText(EditSchoolMienActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    judgeIsNull();

                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                BasexUtilsProtocol.dialog.dismiss();


            }
        });
    }
}
